package com.bw.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.UUID;
import com.bw.entity.OrderInfo;
import com.bw.mapper.OrderInfoMapper;
import com.bw.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderInfoMapper orderInfoMapper;

    @Transactional
    @Override
    public OrderInfo addOrder(OrderInfo orderInfo) {
        //用户账号、来源、支付方式非空校验
        Assert.notNull(orderInfo.getUserId(),"用户账号不为空");
        Assert.notNull(orderInfo.getSourceId(),"来源不为空");
        Assert.notBlank(orderInfo.getPayType(),"支付方式不为空");

        //订单编号自动生成，不能重复
        String orderNum = UUID.randomUUID().toString();
        orderInfo.setOrderNum(orderNum);
        //订单状态，默认为：“未支付”
        if (orderInfo.getStatus() == null){
            orderInfo.setStatus(0);
        }
        //打印方法名称和参数到控制台
        System.out.println("---addOrder------------------");

        orderInfoMapper.addOrder(orderInfo);

        return orderInfo;
    }

    @Override
    public List<OrderInfo> selectList(OrderInfo orderInfo) {

        return  orderInfoMapper.selectList(orderInfo);

    }
}
